package com.cjj.common.entity;

import com.cjj.common.consts.ResConst;
import com.cjj.common.framework.exception.SecurityException;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * @author DoyuTu
 */
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class Res<T> {

    @ApiModelProperty(value = "返回码,200为正常,详情参考http状态码", example = "200")
    private int code;
    @ApiModelProperty(value = "返回描述信息", example = "成功")
    private String message;
    @ApiModelProperty("返回数据")
    private T data;

    private static <T> Res<T> of(int code, String message, T data) {
        return new Res<>(code, message, data);
    }

    public static <T> Res<T> success(T data) {
        return of(ResConst.OK.getCode(), ResConst.OK.getMessage(), data);
    }

    public static <T> Res<T> success() {
        return success(null);
    }

    public static <T> Res<T> fail(ResConst resp) {
        return fail(resp, resp.getMessage());
    }

    public static <T> Res<T> fail(String message) {
        return fail(ResConst.BUSINESS_ERROR, message);
    }

    public static <T> Res<T> fail(ResConst resp, String message) {
        return of(resp.getCode(), message, null);
    }

    public static <T> Res<T> fail(SecurityException.Status status, T data) {
        return of(status.getCode(), status.getMessage(), data);
    }

    public static <T> Res<T> fail(SecurityException e) {
        return of(e.getCode(), e.getMessage(), null);
    }
}
